// utils/storage.ts

const STORAGE_PREFIX = 'myapp_'  // 可以自定义前缀，避免key冲突

function getKey(key: string) {
  return STORAGE_PREFIX + key
}

export function setStorage<T>(key: string, value: T): void {
  try {
    wx.setStorageSync(getKey(key), value)
  } catch (error) {
    console.error('setStorage失败', error)
  }
}

export function getStorage<T>(key: string): T | null {
  try {
    const data = wx.getStorageSync(getKey(key))
    return data ?? null
  } catch (error) {
    console.error('getStorage失败', error)
    return null
  }
}

export function removeStorage(key: string): void {
  try {
    wx.removeStorageSync(getKey(key))
  } catch (error) {
    console.error('removeStorage失败', error)
  }
}
